/* eslint valid-jsdoc: "off" */

'use strict'

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {})

  // 关闭csrf开启跨域
  config.security = {
    // 关闭 csrf
    csrf: {
      enable: false
    },
    // 跨域白名单
    domainWhiteList: ['http://127.0.0.1:5173']
  }
  // 允许跨域的方法
  config.cors = {
    origin: (ctx) => ctx.get('origin'), //允许所有的IP+端口跨域访问
    credentials: true, // 开启认证
    allowMethods: 'GET, PUT, POST, DELETE, PATCH'
  }
  // 日志配置
  config.logger = {
    level: 'NONE' // SILENT, DEBUG, INFO, WARN, ERROR, FATAL
  }
  // sequelize
  config.sequelize = {
    dialect: 'mysql',
    host: '127.0.0.1',
    username: 'root',
    password: '123456',
    port: 3306,
    database: 'zy_time',
    // 中国时区
    timezone: '+08:00',
    define: {
      // 取消数据表名复数
      freezeTableName: true,
      // 自动写入时间戳 created_time updated_time
      timestamps: true,
      // 字段生成软删除时间戳 deleted_time
      // paranoid: true,
      createdAt: 'created_time',
      updatedAt: false,
      // deletedAt: 'deleted_time',
      // 所有驼峰命名格式化
      underscored: true
    }
  }
  // 参数验证
  config.valparams = {
    locale: 'zh-cn',
    throwError: true
  }
  // token
  config.jwt = {
    secret: 'zy_time_jwt'
  }
  // token验证中间件
  config.auth = {
    ignore: ['/register', '/login', '/wall/list', '/leaveword/list', '/comment/list', '/admin']
  }
  config.adminAuth = {
    match: ['/admin']
  }
  // 密码加密
  config.crypto = {
    // 秘钥
    secret: 'zy_time_crypto'
  }
  // oss
  config.multipart = {
    mode: 'file',
    fileSize: '5mb'
  }
  config.oss = {
    client: {
      accessKeyId: 'LTAI5tQadFshKwdp9X9ckm3p',
      accessKeySecret: 'JRPc0PG6sC0g79JkiJ46nhFBOn14YD',
      bucket: 'time-egg',
      endpoint: 'oss-cn-guangzhou.aliyuncs.com',
      timeout: '60s'
    }
  }

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1684473442590_869'

  // add your middleware config here
  config.middleware = ['errorHandler', 'auth', 'adminAuth']

  // add your user config here
  const userConfig = {
    myAppName: 'time-egg'
  }

  return {
    ...config,
    ...userConfig
  }
}
